package com.yy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.bean.Employee;
import com.yy.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/toIndex")
    public String hello(){
        System.out.println("==================");
        return "index";
    }

    //获取所有雇员信息
    @RequestMapping("/getAllEmps")
    public String getAllEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                             Model model){
        System.out.println("pn = " + pn);
        //分页插件的代码
        PageHelper.startPage(pn,3);
        List<Employee> emps = employeeService.getAllEmps();
        //返回值可以放到 PageInfo中
        PageInfo pageInfo =new PageInfo(emps,2);
        //session.setAttribute("emps",emps);
        System.out.println("pageInfo = " + pageInfo);
        //session.setAttribute("page",pageInfo);
        model.addAttribute("page",pageInfo);
        return "allEmps";
    }

    @RequestMapping("/getEmpsByPage")
    @ResponseBody
    public PageInfo getEmpsByPage(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                                  @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize,
                             Model model){
        System.out.println("pn = " + pn);

        PageHelper.startPage(pn,pageSize);
        List<Employee> emps = employeeService.getAllEmps();
        //return emps;
        PageInfo pageInfo =new PageInfo(emps,2);
        //session.setAttribute("emps",emps);
        System.out.println("pageInfo = " + pageInfo);
        //session.setAttribute("page",pageInfo);
        model.addAttribute("page",pageInfo);
        return pageInfo;
    }

    @RequestMapping("/getAllEmps2")
    //把返回的数据变成json 格式的数据
    @ResponseBody
    public List<Employee> getAllEmps2(){
       return employeeService.getAllEmps();
    }

    @RequestMapping("/getEmpsByCondition")
    public String getEmpsByEmail(String emial){
        employeeService.getEmpsByEmail(emial);
        return "success";
    }

    @RequestMapping("/editEmpById")
    @ResponseBody
    public Map<String,String> editEmpById(@RequestBody Employee employee){
        Map<String,String> map =new HashMap<>();
        System.out.println("employee = " + employee);
        int i = employeeService.updateEmpById(employee);
        map.put("msg","success");
        return map;
    }

    @RequestMapping(value = "/getEmpByName",method = RequestMethod.GET)
    @ResponseBody
    public List<Employee> getEmpByName(String empName){
        if("".equals(empName)){
            return employeeService.getAllEmps();
        }
        System.out.println("empName = " + empName);

        return employeeService.getEmpsByName(empName);
    }

   // @RequestMapping("/deleteEmpById")
    @DeleteMapping("/deleteEmpById")
    @ResponseBody
    public Map<String,String> deleteEmpById(Integer empId){
        System.out.println("empId = " + empId);
        int i = employeeService.deleteEmpById(empId);
        Map<String,String> map =new HashMap<>();
        if(i>0){
            map.put("msg","success");
        }else {
            map.put("msg","fail");
        }
        return map;
    }
    @DeleteMapping("/abc")
    @ResponseBody
    public String abc(){
        System.out.println("=========");
        return "hello";
    }

    @DeleteMapping("/bcd")
    @ResponseBody
    public String bcd(){
        System.out.println("=========");
        return "hello";
    }

    @DeleteMapping("/ddd")
    @ResponseBody
    public String ddd(){
        System.out.println("====ddd=====");
        return "ddd";
    }


}
